﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace RichFrame.UIFrame
{
    public class UIManager : MonoSingleton<UIManager>
    {
        public Dictionary<string, UIForm> uiForms = new Dictionary<string, UIForm>();
        [SerializeField]
        List<UIForm> showingForms = new List<UIForm>();

        /// <summary>
        /// 注册一个UI面板
        /// </summary>
        /// <param name="formName"></param>
        /// <param name="uiForm"></param>
        public void RegisterUIForm(string formName, UIForm uiForm)
        {
            uiForms.Add(formName, uiForm);
        }

        public void RemoveUIForm(string formName)
        {
            uiForms.Remove(formName);
        }

        /// <summary>
        /// 显示UI
        /// </summary>
        /// <param name="formName"></param>
        public void ShowForm(string formName)
        {
            UIForm f;
            if (uiForms.TryGetValue(formName, out f))
            {
                if(showingForms.Count > 0 && showingForms[showingForms.Count - 1] == f)
                {
                    return;
                }
                f.Display();
                switch (f.showType)
                {
                    case UIForm.ShowType.Normal:
                        if (showingForms.Count > 0)
                        {
                            var topForm = showingForms[showingForms.Count - 1];
                            if(topForm != f)
                            {
                                topForm.gameObject.SetActive(false);
                            }
                        }
                        break;
                    case UIForm.ShowType.Popup:
                        SetupPopupComp(f);
                        break;
                    case UIForm.ShowType.HideOther:
                        HideAll();
                        break;
                    default:
                        break;
                }
                showingForms.Add(f);
            }
        }
        /// <summary>
        /// 隐藏UI
        /// </summary>
        /// <param name="formName"></param>
        public void HideForm(string formName)
        {
            UIForm f;
            if (uiForms.TryGetValue(formName, out f))
            {
                showingForms.Remove(f);
                switch (f.showType)
                {
                    case UIForm.ShowType.Normal:
                        if(showingForms.Count > 0)
                        {
                            var form = showingForms[showingForms.Count - 1];
                            form.gameObject.SetActive(true);
                        }
                        break;
                    case UIForm.ShowType.Popup:
                        RemovePopupComp(f);
                        break;
                    case UIForm.ShowType.HideOther:
                        ShowAll();
                        break;
                    default:
                        break;
                }
                f.Close();
            }
        }

        void HideAll()
        {
            foreach (var item in showingForms)
            {
                item.gameObject.SetActive(false);
            }
        }

        void ShowAll()
        {
            foreach (var item in showingForms)
            {
                item.gameObject.SetActive(true);
            }
        }

        void SetupPopupComp(UIForm uIForm)
        {
            Canvas canvas = uIForm.gameObject.AddComponent<Canvas>();
            canvas.overrideSorting = true;
            canvas.sortingOrder = 20000;
            GraphicRaycaster graphicRaycaster = uIForm.gameObject.AddComponent<GraphicRaycaster>();
        }

        void RemovePopupComp(UIForm uIForm)
        {
            GraphicRaycaster graphicRaycaster = uIForm.gameObject.GetComponent<GraphicRaycaster>();
            Destroy(graphicRaycaster);
            Canvas canvas = uIForm.gameObject.GetComponent<Canvas>();
            Destroy(canvas);
        }
    }
}
